<template>
  <el-container>
    <el-main style="padding: 10px;">

      <el-card shadow="always" body-style="padding: 0px; min-height: 500px;">
      <el-container>
          <el-header class="topbar">
              <div class="left-panel">
                  <el-button type="success" icon="el-icon-plus" @click="save()">创建新分组</el-button>
              </div>
              <div class="right-panel">
                  <div class="right-panel-search">
                      <el-input v-model="searchForm.searchText" placeholder="名称 / 备注" style="width: 250px;"></el-input>
                      <el-button type="primary" icon="el-icon-search" @click="getList()"></el-button>
                      <el-button type="warning" icon="el-icon-refresh" @click="getList()"></el-button>
                  </div>
              </div>
          </el-header>

          <el-main>
              <el-table
              :data="pageResult.list"
              element-loading-text="加载数据中"
              v-loading="loading"
              :header-row-style="{height:45+'px'}"
              :header-cell-style="{background:'#F2F6FC'}"
              :row-style="{height:60+'px'}"
              style="width: 100%; min-height: 450px;">
                  <el-table-column prop="groupName"          label="分组名称" ></el-table-column>
                  <el-table-column prop="groupDesc"  label="备注" ></el-table-column>
                  <el-table-column prop="configFileCount"  label="含有配置文件" ></el-table-column>
                  <el-table-column prop="createTime"  label="创建时间" ></el-table-column>
                  <el-table-column label="操作" width="250">
                      <template slot-scope="scope">
                          <el-button
                              type="text"
                              icon="el-icon-files"
                              @click="configFile(scope.$index, scope.row)">管理配置</el-button>
                          <el-button
                              type="text"
                              icon="el-icon-edit"
                              @click="update(scope.row)">编辑</el-button>
                          <el-button
                              type="text"
                              style="color: #F56C6C"
                              icon="el-icon-delete"
                              @click="deleteInRow(scope.$index, scope.row)">删除</el-button>        
                      </template>
                  </el-table-column>
                  <template #empty>
                      <el-empty :description="'暂无数据'" :image-size="100"></el-empty>
                  </template>
              </el-table>

              <pagination :total="pageResult.total" @page-change="pageChange()"></pagination>
          </el-main>

      </el-container>
    </el-card>    

    </el-main>
    
    <config-save-update ref="ConfigSaveUpdate" @success="success"></config-save-update>

  </el-container>
</template>

<script>
import ConfigSaveUpdate from './modules/config_save_update.vue'

export default {
    components: {
        ConfigSaveUpdate
    },
    data() {
        return {
            searchForm: {
                searchText: ''
            },
            pageResult: { 
                total: 0,
                pageNum: 1,
                pageSize: 10,
                list: []
            },
            loading: false
        }
    },
    mounted() {
        // 初始化列表
        this.getList()
    },
    methods: {
        /**
         * @title 获取列表
         */
        async getList() {
            this.loading = true
            try {
                let response = await this.$API.configGroup.getList(this.searchForm)   
                this.pageResult = response.data
            } finally{
                this.loading = false
            }
        },

        /**
         * @title 更改分页
         */
        pageChange(pageNum, pageSize) {
            this.searchForm.pageNum = pageNum
            this.searchForm.pageSize = pageSize
            this.getList()
        },


        configFile(index, row) {
            this.$router.push({ path: '/config/file', query: { configGroupId: row.id, groupName: row.groupName }})
        },

        save() {
            this.$nextTick(() => {
                this.$refs.ConfigSaveUpdate.open({}) 
            })
        },

        update(data) {
            this.$nextTick(() => {
                this.$refs.ConfigSaveUpdate.open(data) 
            })
        },

        success() {
            this.getList()
        },


        deleteInRow(index, row) {
            this.$confirm('此操作将会删除该配置分组，是否继续?', '操作提示', {
                confirmButtonText: '继续',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(() => {
                
                this.$API.configGroup.deleteById(row.id).then(response => {
                    this.getList()
                    this.$message.success(response.message)
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
    }
}
</script>

<style lang="scss" scoped>
.el-main {
    width: 100%;
    height: 100%;
    padding: 0;
}
.topbar {
    height: 50px;
    border-bottom: 1px solid #ebeef5; 
    background: #F2F6FC;
    box-shadow: 0 1px 4px rgba(0,21,41,.08);
    display: flex;
    justify-content:space-between;

    .left-panel {
        display: flex;
        align-items: center;
    }
    .right-panel {
        display: flex;
        align-items: center;
    }

    .right-panel-search {display: flex;align-items: center;}

    .right-panel-search > * + * {margin-left:10px;}
}
</style>